home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
ada
/
gwuada_9.zip
/
PRSERR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-27
|
39KB
|
1,388 lines
/*
* Copyright (C) 1985-1992 New York University
*
* This file is part of the Ada/Ed-C system. See the Ada/Ed README file for
* warranty (none) and distribution info and also the GNU General Public
* License for more details.
*/
/********************************************************************
*
* *****************
* * P R S E R R
* *****************
*
* Written by
* Brian Siritzky
* 1983
*
********************************************************************/
/********************************************************************/
/********************************************************************/
#include "hdr.h"
#include "ada.h"
#include "adalexp.h"
#include "actionp.h"
#include "pspansp.h"
#include "errsp.h"
#include "lookupp.h"
#include "prsutilp.h"
#include "recoverp.h"
#include "makecorp.h"
#include "prserrp.h"
static void get_poss_tok();
static void get_next(int);
/*
* Variables needed for scope and secondary recoveries
*/
struct two_pool *closer_toksyms ;
struct two_pool *closer_bottom ;
struct two_pool *POSS_TOK;
/* struct prsstack **tokens; deleted on Sam's suggestion 12-11-84 */
int TOKSYMS[S_TOKSYMS], n_TOKSYMS;
int BACKUPTOKS = 0;
int MAX_CHECK = MIN_LEN ;
PCAND next_c, y, next_cand, CANDIDATES[C_BOUND];
static PCAND tmp_cand,temp_cand;
int n_CANDIDATES[C_BOUND] ;
int nps;
int n_open_seq;
int *open_seq;
int n_closer_toksyms;
void prserr(struct prsstack *curtok) /*;prserr*/
{
/********************************************************************
*
* This routine is the syntactic error recovery routine. It at-
* tempts to correct simple errors and when that is not possible,
* deletes tokens possibly to the left and to the right of the error
* point until the parse can safely resume. The process is thus di-
* vided naturally into two parts, called primary and secondary
* recovery. Both primary and secondary recovery include efforts at
* "scope recovery": i.e. the closing of lexically open scopes
* through the insertion of one or more closer token sequences. Ex-
* amples of such sequences are right parentheses, "END ;", and "END
* IF;".
*
* Primary recovery consists of the simple corrections - merging to-
* kens, substituting a token (including a reserved word that is
* misspelled as an identifier), inserting a token, and deleting a
* token - along with scope recovery.
*
* An attempt at simple correction at either the error token or at
* some parse stack element is called a trial.
*
* For the first trial simple corrections are attempted at the token
* at which the error was detected (the error token), with the parse
* in the configuration obtaining just after the shifting of the
* previous token. In order to back up to the succeeding trial, the
* top elements are peeled from the state and parse stacks, with the
* top element of the parse stack appended to the front of the input
* token sequence. Again attempts at simple correction are made. The
* process is repeated until the determined extent of the backup
* move has been accomplished.
*
* The criterion by which the effectiveness of a simple correction
* candidate is measured is the distance it allows the parse to pro-
* gress into the forward context (up to MAX_LOOK_AHEAD = 25 to-
* kens). During the simple correction trials we gather together
* the CANDIDATES that allow the parse to progress the furthest,
* provided that an advance of at least MIN_CHECK is accomplished
* (if not, then CANDIDATES is empty and simple correction fails).
* If CANDIDATES is not empty, it is pruned in accordance with cer-
* tain restrictions described below, and then one of them is chosen
* as the appropriate correction provided that a condition described
* below is satisfied.
*
* No attempt is made to delete or substitute for a nonterminal.
*
* If no simple correction is chosen, scope recovery is attempted at
* each point at which simple recovery was attempted. The scope
* recovery procedure determines whether the insertion of a sequence
* of scope closers allows the parse to progress MIN_LEN distance
* into the forward context. If so, this multiple insertion is
* chosen as the correction candidate.
*
* If scope recovery also fails, then secondary recovery is invoked.
* The parse is restored to the configuration obtaining upon en-
* trance to PRSERR, and each parse stack element is tested - in se-
* quence from the top - to see whether parsing can resume from that
* point, perhaps with the inclusion of one or more closer token se-
* quences. The extent of the advance required in order for the
* parse to be regarded as successfully resumed depends upon the
* current token, but is at least MIN_LEN.
*
* If secondary recovery at the current token does not succeed, it
* is ignored and the next one obtained and the same check is re-
* peated. Eventually, there must be an input for which the parse
* can continue, because the end of file token, EOFT, is compatible
* with a state on the state stack.
*
* When the recovery point is found, control is returned to the
* parser. It is assured that parsing can continue beyond the error
* token.
*
********************************************************************/
/* PARSE STACK and BINARY TUPLE STRUCTURES */
typedef struct two_pool *TUPLE;
struct prsstack *ERROR_TOKEN;
struct prsstack *oldprevtok;/* Saved for scope and secondary recovery */
struct prsstack *tmp_ps;
TUPLE STATE_STACK = NULL;
TUPLE prs_toks = NULL; /* Used to determine the no of */
TUPLE tmp_tp; /* trials to be performed */
/* Used for freeing storage */
TUPLE prs_stack_syms = NULL;
int pb; /* Used as a flag to check for a parse block */
int threshold, /* Used to prune candidates */
exists; /* Flag used in list searching */
short trials, /* Number of trials performed */
j, r, trial,
i, /* Loop and auxilliary */
bk, cc, x, si ;
int n_PARSE_STACK;
int save_n_TOKSYMS ;
struct two_pool *states;
int n_single_cand_modes, total_CANDIDATES;
int n_STATE_STACK, n_sta_stack;
ERROR_TOKEN = copytoken (curtok);
MAX_CHECK = MIN_LEN;
/* ERR_MSGS = NULL ; CLOSER_TOKSYMS = NULL ; */
copystack (sta_stack, &STATE_STACK, &n_sta_stack);
/* save for scope and secondary recovery */
if (PREVTOK != NULL)
oldprevtok = copytoken (PREVTOK);
n_STATE_STACK = n_sta_stack;
BACKUPTOKS = 0;
get_next (MAX_LOOK_AHEAD);
/* prs_stack_syms := [r(1) : r in PRS_STACK];
* Loop over PRS_STACK, collecting the symbols in a list
* headed by prs_stack_syms. While doing this, count up the
* number of elements in the parse stack, keeping this in
* n_PARSE_STACK and nps
*/
n_PARSE_STACK = 0;
if ((tmp_ps= prs_stack) == NULL)/* Check for empty stacks */
prs_stack_syms = NULL;
else { /* otherwise copy the list */
/* Treat the first element as a special case */
tmp_tp = prs_stack_syms = TALLOC ();
tmp_tp -> link = NULL;
tmp_tp -> val.state = tmp_ps -> symbol;
n_PARSE_STACK = 1;
/* Loop over the rest of the stack */
while (tmp_ps -> prev != NULL) {
tmp_tp -> link = TALLOC ();
tmp_tp = tmp_tp -> link;
tmp_ps = tmp_ps -> prev;
tmp_tp -> val.state = tmp_ps -> symbol;
n_PARSE_STACK++;
} /* while */
tmp_tp -> link = NULL;
} /* else */
nps = n_PARSE_STACK;
/* * TOKSYMS := [t(1) : t in TOKENS];
* Loop over TOKENS, collecting the symbols in a list
* The integer tokind is a count of the number of
* elements in the array tokens.
*
* Note that tokens[tokind] is the next token, so we must
* reverse the order of TOKSYMS
*/
/* Put the current token at the top of the token stack */
tokens[tokind = (tokind + 1) % toksiz] = curtok;
i = 0;
j = tokbottom;
for (;;) {
TOKSYMS[i] = tokens[j]->symbol;
if (j == tokind)
break;
j = (j + 1) % toksiz;
i++;
}
save_n_TOKSYMS = n_TOKSYMS = (toksiz